package com.kingdee.pda.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.kingdee.pda.callback.HavePermissionCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限控制工具类
 *
 * @author LiJiaJian
 * @date 16/10/19 上午11:48
 */
public class PermissionUtil {

    /**
     * Activity检查权限
     *
     * @param activity
     * @param permission
     * @param requestCode
     * @param callback
     */
    public static void checkPermissionByActivity(Activity activity, String permission, int requestCode, HavePermissionCallback callback) {
        //检查权限
        if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
            //进入到这里代表没有权限.
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
                //已经禁止提示了
                Toast.makeText(activity, "您已禁止该权限，需要重新开启。", Toast.LENGTH_SHORT).show();
            }
            ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);
        } else {
            callback.onHavePermissionCallback(permission, requestCode);
        }
    }
    /**
     * Activity检查权限,同时申请多个权限
     *
     * @param activity  上下文
     * @param permission 权限列表 数组
     * @param requestCode 请求的code
     * @param callback  回调
     */
    public static void checkPermissionsByActivity(Activity activity, String[] permission, int requestCode, HavePermissionCallback callback) {
        //检查权限
//        if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
//            //进入到这里代表没有权限.
//            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
//                //已经禁止提示了
//                Toast.makeText(activity, "您已禁止该权限，需要重新开启。", Toast.LENGTH_SHORT).show();
//            } else {
//                ActivityCompat.requestPermissions(activity, permission, requestCode);
//            }
//        } else {
//            callback.onHavePermissionCallback(permission, requestCode);
//        }
        // 声明一个集合，在后面的代码中用来存储用户拒绝授权的权
        List<String> mPermissionList = new ArrayList<>();
        for (String aPermission : permission) {
            if (ContextCompat.checkSelfPermission(activity, aPermission) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(aPermission);
            }
        }
        if (mPermissionList.isEmpty()) {//未授予的权限为空，表示都授予了
            callback.onHavePermissionCallback("", requestCode);
        } else {//请求权限方法
            //已经禁止提示了
            Toast.makeText(activity, "您存在禁用的权限，需要去设置重新开启。", Toast.LENGTH_SHORT).show();
            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
            ActivityCompat.requestPermissions(activity, permissions, requestCode);
        }
    }
    /**
     * Fragment 检查权限
     *
     * @param context
     * @param fragment
     * @param permission
     * @param requestCode
     * @param callback
     */
    public static void checkPermissionByFragment(Context context, Fragment fragment, String permission, int requestCode, HavePermissionCallback callback) {
        //检查权限
        if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
            //进入到这里代表没有权限.
            if (fragment.shouldShowRequestPermissionRationale(permission)) {
                //已经禁止提示了
                Toast.makeText(context, "您已禁止该权限，需要重新开启。", Toast.LENGTH_SHORT).show();
            } else {
                fragment.requestPermissions(new String[]{permission}, requestCode);
            }
        } else {
            callback.onHavePermissionCallback(permission, requestCode);
        }
    }

    /**
     * 判断摄像头是否可用
     * 主要针对6.0 之前的版本，现在主要是依靠try...catch.
     * 以后有更好的方法的话可适当替换
     *
     * @return
     */
    public static boolean isCameraCanUse() {
        boolean canUse = true;
        Camera mCamera = null;
        try {
            mCamera = Camera.open();
            // setParameters 是针对魅族MX5 做的。MX5 通过Camera.open() 拿到的Camera
            // 对象不为null
            Camera.Parameters mParameters = mCamera.getParameters();
            mCamera.setParameters(mParameters);
        } catch (Exception e) {
            canUse = false;
        }
        if (mCamera != null) {
            mCamera.release();
        }
        return canUse;
    }

}
